/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.banner;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.CommonDialog;

import com.xuexiang.xui_lib.component.banner.SimpleImageBanner;
import com.xuexiang.xui_lib.component.banner.anim.select.RotateEnter;
import com.xuexiang.xui_lib.component.banner.anim.select.ZoomInEnter;
import com.xuexiang.xui_lib.component.banner.interfaces.OnItemClickListener;
import com.xuexiang.xui_lib.component.banner.transform.ZoomOutSlideTransformer;
import com.xuexiang.xui_lib.component.banner.widget.BannerItem;
import com.xuexiang.xuidemo.MainAbility;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.page.base.BasePageAbilitySlice;
import com.xuexiang.xuidemo.page.base.Page;
import com.xuexiang.xuidemo.provider.DemoDataProvider;
import com.xuexiang.xuidemo.provider.ViewPagerItemSelectProvider;
import com.xuexiang.xuidemo.util.XToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * ViewPager实现的Banner
 *
 * @since 2021-04-16
 */
@Page(name = "使用ViewPager实现的Banner", action = "ViewPagerBannerSlice", ability = BannerAbility.class)
public class ViewPagerBannerSlice extends BasePageAbilitySlice implements
    Component.ClickedListener, OnItemClickListener {
    private List<BannerItem> mData;
    private Text tvUserGuide;
    private Text tvSelectTransformer;
    private Text tvSelectTransformer2;
    private SimpleImageBanner sib_simple_usage;
    private SimpleImageBanner rib_simple_usage;
    private SimpleImageBanner sib_the_most_comlex_usage;
    private SimpleImageBanner sib_res;
    private SimpleImageBanner sib_rectangle;
    private SimpleImageBanner sib_corner_rectangle;
    private SimpleImageBanner sib_indicator_right_with_text;
    private SimpleImageBanner sib_indicator_left_with_text;
    private SimpleImageBanner sib_anim;
    private SimpleImageBanner sib_anim2;
    private SimpleImageBanner stb;

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_viewpager_banner;
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        initViews();
        initListener();
    }

    private void initViews() { // 初始化View
        mData = DemoDataProvider.getBannerList();
        sib_simple_usage = (SimpleImageBanner) findComponentById(ResourceTable.Id_sib_simple_usage);
        rib_simple_usage = (SimpleImageBanner) findComponentById(ResourceTable.Id_rib_simple_usage);
        sib_the_most_comlex_usage = (SimpleImageBanner) findComponentById(ResourceTable.Id_sib_the_most_comlex_usage);
        sib_res = (SimpleImageBanner) findComponentById(ResourceTable.Id_sib_res);
        sib_rectangle = (SimpleImageBanner) findComponentById(ResourceTable.Id_sib_rectangle);
        sib_corner_rectangle = (SimpleImageBanner) findComponentById(ResourceTable.Id_sib_corner_rectangle);
        sib_indicator_right_with_text = (SimpleImageBanner)
            findComponentById(ResourceTable.Id_sib_indicator_right_with_text);
        sib_indicator_left_with_text = (SimpleImageBanner)
            findComponentById(ResourceTable.Id_sib_indicator_left_with_text);
        sib_anim = (SimpleImageBanner) findComponentById(ResourceTable.Id_sib_anim);
        sib_anim2 = (SimpleImageBanner) findComponentById(ResourceTable.Id_sib_anim2);
        stb = (SimpleImageBanner) findComponentById(ResourceTable.Id_stb);

        tvUserGuide = (Text) findComponentById(ResourceTable.Id_tv_user_guide);
        tvSelectTransformer = (Text) findComponentById(ResourceTable.Id_tv_select_transformer);
        tvSelectTransformer2 = (Text) findComponentById(ResourceTable.Id_tv_select_transformer2);
        sib_simple_usage();
        sib_the_most_comlex_usage();
        sib_res();
        sib_rectangle();
        sib_corner_rectangle();
        sib_indicator_right_with_text();
        sib_indicator_left_with_text();
        sib_anim();
        sib_anim2();
        stb();
    }

    private void sib_simple_usage() {
        sib_simple_usage.setDataList(mData).configData().startScroll(); // 图片轮播的简单使用
        rib_simple_usage.setDataList(mData).configData().startScroll(); // 图片自定义使用
    }

    private void sib_the_most_comlex_usage() { // 图片轮播的复杂使用【参数配置】
        sib_the_most_comlex_usage
            .setDataList(mData)
            .setSelectAnimClass(ZoomInEnter.class)
            .setTransformer(ZoomOutSlideTransformer.class)
            .setOnItemClickListener(this)
            .configData();
        sib_the_most_comlex_usage.startScroll();
    }

    private void sib_res() { // 索引点使用资源图片
        sib_res
            .setDataList(mData)
            .setOnItemClickListener(this)
            .configData();
        sib_res.startScroll();
    }

    private void sib_rectangle() { // 矩形的索引点
        sib_rectangle.setDataList(mData).setOnItemClickListener(this).configData();
        sib_rectangle.startScroll();
    }

    private void sib_corner_rectangle() { // 扁长方形的索引点
        sib_corner_rectangle.setDataList(mData).setOnItemClickListener(this).configData();
        sib_corner_rectangle.startScroll();
    }

    private void sib_indicator_right_with_text() { // 文字在左，索引点在右[xml中设置]
        sib_indicator_right_with_text.setDataList(mData).setOnItemClickListener(this).configData();
        sib_indicator_right_with_text.startScroll();
    }

    private void sib_indicator_left_with_text() { // 文字在右，索引点在左[xml中设置]
        sib_indicator_left_with_text.setDataList(mData).setOnItemClickListener(this).configData();
        sib_indicator_left_with_text.startScroll();
    }

    private void sib_anim() { // 设置选中后切换的动画
        sib_anim
            .setDataList(mData)
            .setSelectAnimClass(ZoomInEnter.class) // 要在配置数据之前先设置动画,不然没效果
            .setOnItemClickListener(this)
            .configData();
        sib_anim.startScroll();
    }

    private void sib_anim2() {
        sib_anim2
            .setDataList(mData)
            .setSelectAnimClass(RotateEnter.class)
            .setOnItemClickListener(this)
            .configData(); // 配置数据
        sib_anim2.startScroll();
    }

    private void stb() { // 简单的文字轮播
        stb.setDataList(mData)
            .setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(Component view, String item, int position) {
                    XToastUtils.toast("position--->" + position);
                }
            })
            .configData();
        stb.startScroll();
    }

    /**
     * 初始化监听
     */
    private void initListener() {
        tvUserGuide.setClickedListener(this);
        tvSelectTransformer.setClickedListener(this);
        tvSelectTransformer2.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_tv_user_guide: // 跳转到启动页
                jumpSplash(0);
                break;
            case ResourceTable.Id_tv_select_transformer:
                showSelectDialog(false);
                break;
            case ResourceTable.Id_tv_select_transformer2:
                showSelectDialog(true);
                break;
            default:
                break;
        }
    }

    // 跳转到启动页
    private void jumpSplash(int position) {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withBundleName(getBundleName())
            .withAbilityName(MainAbility.class)
            .build();
        intent.setParam("position", position);
        intent.setParam("ViewPagerBanner", true);
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void showSelectDialog(final boolean isSimpleImageBanner) {
        List<String> itemList = new ArrayList<>();
        for (Class<?> cc : DemoDataProvider.transformers()) {
            itemList.add(cc.getSimpleName());
        }
        CommonDialog commonDialog = new CommonDialog(this);
        LayoutScatter instance = LayoutScatter.getInstance(this);
        Component parse = instance.parse(ResourceTable.Layout_ability_viewpager_select_dialog, null, false);
        Component component = parse.findComponentById(ResourceTable.Id_component);
        ListContainer listContainer = (ListContainer) parse.findComponentById(ResourceTable.Id_viePagerListContainer);
        ViewPagerItemSelectProvider provider = new ViewPagerItemSelectProvider(itemList);
        listContainer.setItemProvider(provider);
        provider.setOnItemClick(new ViewPagerItemSelectProvider.OnItemClickListener() {
            @Override
            public void setItemChange(int position) {
                jumpSplash(position);

                if (commonDialog != null && commonDialog.isShowing()) {
                    commonDialog.destroy();
                }
            }
        });
        component.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (commonDialog != null && commonDialog.isShowing()) {
                    commonDialog.destroy();
                }
            }
        });
        commonDialog.setContentCustomComponent(parse);
        commonDialog.setTransparent(true);
        int size = ComponentContainer.LayoutConfig.MATCH_PARENT;
        commonDialog.setSize(size, size);
        commonDialog.show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        sib_simple_usage.stopTurning();
        rib_simple_usage.stopTurning();
        sib_res.stopTurning();
        sib_rectangle.stopTurning();
        sib_corner_rectangle.stopTurning();
        sib_indicator_right_with_text.stopTurning();
        sib_indicator_left_with_text.stopTurning();
        stb.stopTurning();
    }

    @Override
    public void onItemClick(Component view, String item, int position) {
        XToastUtils.toast("position--->" + position + ", item:" + item);
    }
}
